#!/usr/bin/ruby

class Menu
  attr_reader :up_item
  
  def initialize(name,up_item=nil)
    @name = name
    self.up_item = up_item
    @items = {}
  end
  
  def up_item=(item)
    if item
      @up_item = item
      item.down_menu = self
    end
  end
  
  def add_item(item)
    @items[item.name] = item
    item.belong_menu = self
  end
  
  def [](item_name)
    @item[item_name]
  end
  
  def to_s
    str = ""
    str << "#{@name}:\n"
    @items.each {|name,item| str << "[#{item.to_s}] "}
    str
  end
end

class MenuItem
  attr_reader :down_menu
  attr_accessor :belong_menu
  attr_reader :name
  
  def initialize(name,belong_menu,down_menu=nil)
    @name = name
    belong_menu.add_item(self) if belong_menu
    self.down_menu = down_menu
  end
  
  def down_menu=(menu)
    if menu
      @down_menu = menu
      #menu.up_item = self
    end
  end
  
  def to_s
    str = ""
    str << "#{@name}"
    str << "[#{@down_menu.to_s}]" if down_menu
    str
  end
end